package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.CreateLiveBean;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.LiveConfigPresenter;
import com.tj.jinzao.utils.DateFormatUtils;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.MaxTextLengthFilter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.luck.picture.lib.config.PictureConfig.CHOOSE_REQUEST;

/**
 * 直播申请界面
 * Created by wjy on 2019/1/5.
 */
public class MyIdentificationLiveActivity extends BaseActivity<LiveConfigPresenter> implements IUploadView<CreateLiveBean>,View.OnClickListener {
    @BindView(R.id.passWord)
    EditText passWord;
    @BindView(R.id.bt_submit)
    Button bt_submit;
    @BindView(R.id.iv_card_1)
    ImageView iv_card_1;
    @BindView(R.id.theme)
    EditText theme;
    @BindView(R.id.rl_live_time_layout)
    RelativeLayout rl_live_time_layout;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.rl_identification_card_layout)
    RelativeLayout rl_identification_card_layout;
    private String mImagePath;
    private User currentUser;

    @Override
    protected LiveConfigPresenter createPresenter() {
        return new LiveConfigPresenter(this);
    }

    @Override
    protected void findId() {
        currentUser = UserManager.getInstance().getUser();
        String beginTime = "2018-10-17 18:00";
        String endTime = DateFormatUtils.long2Str(System.currentTimeMillis(), true);
        time.setText(endTime);
        theme.setFilters(new InputFilter[]{new MaxTextLengthFilter(12, MyIdentificationLiveActivity.this,"直播主题限12字以内")});

    }


    private void uploadFile() {
        if (!TextUtils.isEmpty(mImagePath)) {
            if (mImagePath.startsWith(Constant.SD_ROOT)) {
                mPresenter.uploadFile(mImagePath);
                return;
            }
        }
        String theames = theme.getText().toString().trim();
        String times = time.getText().toString().trim();
        String passWords = passWord.getText().toString().trim();
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("uid",currentUser.getId());
        hashMap.put("liveTime",times);
        hashMap.put("title",theames);
        hashMap.put("password",passWords);
        hashMap.put("imgurl",mImagePath);
        mPresenter.sendLive(hashMap);
    }
    private TimePickerView pvTime;
    private void getTime(){
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);

        //正确设置方式 原因：注意事项有说明
        startDate.set(1960,0,1);
        endDate.set(2100,11,31);
        pvTime = new TimePickerBuilder(MyIdentificationLiveActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                time.setText(sdf.format(date));
            }
        }).setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
//                .setCancelText("Cancel")//取消按钮文字
//                .setSubmitText("Sure")//确认按钮文字
//                .setContentSize(18)//滚轮文字大小
//                .setTitleSize(20)//标题文字大小
//                .setTitleText("Title")//标题文字
//                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                .isCyclic(true)//是否循环滚动
//                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.GRAY)//确定按钮文字颜色
                .setCancelColor(Color.GRAY)//取消按钮文字颜色
//                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
//                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setLabel("年","月","日","","","")//默认设置为年月日时分秒
//                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .isDialog(true)//是否显示为对话框样式
                .build();
        pvTime.show();

    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_my_identification_live;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected void initLinstener() {
        rl_identification_card_layout.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        rl_live_time_layout.setOnClickListener(this);

    }



    @Override
    public Activity getContext() {
        return null;
    }

    @Override
    public void onRequestSuccess(CreateLiveBean createLiveBean) {
        ToastUtils.showToast(this,"直播申请提交成功");
        finish();

    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {
        if(error_message!=null&&error_message.equals("00")){
            ToastUtils.showToast(this,"直播申请提交成功");
            finish();
        }else {
            if(error_message!=null&&error_message!=null){
                ToastUtils.showToast(this,error_message);
            }else {
                ToastUtils.showToast(this,"直播申请提交失败");
            }

        }

    }

    @Override
    public void onUploadSuccess(String path, String url) {
        mImagePath=url;
        uploadFile();

    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia localMedia : selectList) {
                        mImagePath = localMedia.isCompressed() ? localMedia.getCompressPath() : localMedia.getPath();
                        Glide.with(this).load(mImagePath).into(iv_card_1);
                    }
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_identification_card_layout:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.bt_submit:
                String name = theme.getText().toString().trim();
                String number = passWord.getText().toString().trim();
                String times = time.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtils.showToast(this, "请输入主题");
                    return;
                } else if (TextUtils.isEmpty(number)) {
                    ToastUtils.showToast(this, "请输入密码");
                    return;
                }else if (TextUtils.isEmpty(times)) {
                    ToastUtils.showToast(this, "请选择时间");
                    return;
                }
                if (TextUtils.isEmpty(mImagePath)) {
                    ToastUtils.showToast(this, "请上传照片");
                    return;
                }
                uploadFile();

                break;
            case R.id.rl_live_time_layout:
                if(ConstantApp.isFastDoubleClick(1500)){
                    return;
                }
                getTime();
                break;
        }
    }
}
